package com.engine.salary.util;

import com.engine.salary.exception.SalaryRunTimeException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @Description: 空指针判断工具类
 * @Author: zhangheng
 * @CreateDate: 2022/1/17 11:36
 * @Version: v1.0
 */
public abstract class SalaryAssert {

    /**
     * 判断入参不为null
     *
     * @param object  待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断多个入参不为null
     *
     * @param message 检查失败返回的异常信息
     * @param objects 待检查参数
     */
    public static void notNull(String message, Object... objects) {
        for (Object obj : objects) {
            if (obj == null) {
                throw new SalaryRunTimeException(message);
            }
        }
    }

    /**
     * 判断入参为null
     *
     * @param object  待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断集合是否为空
     *
     * @param collection 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void isEmpty(Collection<?> collection, String message) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断集合不为空
     *
     * @param collection 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断数组是否为空
     *
     * @param arr     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notEmpty(Object[] arr, String message) {
        if (ObjectUtils.isEmpty(arr)) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断map是否为空
     *
     * @param map     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new SalaryRunTimeException(message);
        }
    }

    /**
     * 判断数组元素是否为空
     *
     * @param arr     待检查参数
     * @param message 检查失败返回的异常信息
     */
    public static void notNullElement(Object[] arr, String message) {
        if (arr != null) {
            for (Object obj : arr) {
                if (obj == null) {
                    throw new SalaryRunTimeException(message);
                }
            }
        }
    }

    /**
     * 判断boolean
     *
     * @param expression 待检查参数
     * @param message    检查失败返回的异常信息
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new SalaryRunTimeException(message);
        }
    }

    public static void isFalse(boolean expression, String message) {
        if (expression) {
            throw new SalaryRunTimeException(message);
        }
    }

    public static void isBlank(CharSequence cs, String message) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    throw new SalaryRunTimeException(message);
                }
            }
        }
    }

    public static void notBlank(CharSequence cs, String message) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (Character.isWhitespace(cs.charAt(i))) {
                    throw new SalaryRunTimeException(message);
                }
            }
        }
        if (cs == null || cs.length() == 0) {
            throw new SalaryRunTimeException(message);
        }

    }
}
